<template>
  <div>
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="addUserForm"
        :rules="rules"
        ref="addUserFormRef"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="用户名" prop="username">
                <el-input v-model="addUserForm.username"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="密码" prop="password">
                <el-input
                  v-model="addUserForm.password"
                  show-password
                ></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="addUserForm.name"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <el-form-item label="电话" prop="mobile">
                <el-input v-model="addUserForm.mobile"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="success" @click="submitForm('addUserForm')"
            >创建</el-button
          >
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addUser } from 'api/userlist'
export default {
  name: 'AddUser',
  data () {
    return {
      dialogVisible: false,
      addUserForm: {
        username: '',
        password: '',
        name: '',
        mobile: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'change'
          }
        ],
        name: [{ required: true, message: '姓名不能为空', trigger: 'change' }],
        mobile: [{ required: true, message: '手机不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 创建新用户
    submitForm (submitForm) {
      this.$refs.addUserFormRef.validate(valid => {
        if (!valid) {
          return
        }
        addUser(this.addUserForm)
          .then(res => this.addUserSuccess(res))
          .catch(err => this.addUserFailed(err))
      })
    },
    // 创建新用户成功回调方法
    addUserSuccess (res) {
      console.log(res.data)
      if (res.data.code === 1) {
        return this.$message.error('用户名已经存在，请重试！')
      } else if (res.data.code === 0) {
        this.dialogVisible = false
        this.$message.success('用户添加成功！')
        this.$emit('getUserList')
      } else {
        this.$message.error('用户创建失败！请联系管理员！')
      }
    },
    // 创建新用户失败
    addUserFailed (err) {
      console.log(err)
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    resetForm () {
      this.$refs.addUserFormRef.resetFields()
    }
  }
}
</script>
